package me.firstapp.repository.weixin.jinbi.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.module.weixin.jinbi.JinBiAccount;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.weixin.jinbi.JinBiAccountRepository;

@Repository
public class JinBiAccountRepositoryImpl extends EntityRepositoryImpl<JinBiAccount> implements JinBiAccountRepository {

	@Override
	protected Class<JinBiAccount> getPersistentClass() {
		return JinBiAccount.class;
	}

	@Override
	public JinBiAccount findJinBiAccountByCardNoAndOpenId(String cardNo, String openId) {
		StringBuilder sql = new StringBuilder(" FROM JinBiAccount bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.cardNo = :cardNo ");
		parameters.put("cardNo", cardNo);

		sql.append(" AND bean.openId = :openId ");
		parameters.put("openId", openId);

		List<JinBiAccount> result = super.query(JinBiAccount.class, sql.toString(), parameters);

		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public List<JinBiAccount> findJinBiAccountsForRank(int limitCount) {
		StringBuilder sql = new StringBuilder(" FROM JinBiAccount bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" ORDER BY bean.balance DESC ");
		return super.queryForLimit(JinBiAccount.class, sql.toString(), parameters, limitCount);
	}

}
